<?php
namespace app\wx\controller;
use think\Controller;
//use think\Request;
use think\config;

// use think\Db;
class Upload2 extends Controller
{
   //获取微信access_token
   public  function getaccess_token(){
	  //appid与appsecret改成你自己的
	  $appid = 'wx9aacf08f3fbd2e37';
	  $appsecret = '821d788d0fe0d79b5ae414d1a0b93325';
	  $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
	  $ch = curl_init();
	  curl_setopt($ch, CURLOPT_URL,$url);
	  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
	  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
	  $data = curl_exec($ch);
	  curl_close($ch);
	  $data = json_decode($data,true);
	  return $data['access_token'];
   }

 //设置与发送模板信息
 public function set_msg(){
    //获取access_token
    $access_token = $this->getaccess_token();
    //这里是在模板里修改相应的变量
    $formwork = '{
           "touser":"oUwuW1e-P9EwJ1KsAaie7Ux3MN7E",
           "template_id":"DmJfYJLSuSFGdpsSFkGL3mNmglqZLNCZ494r0Z5Gfqk",
           "url":"http://www.wangwenxiao.com",            
           "data":{
                   "keyword1": {
                       "value":"财税筹划",
                       "color":"#173177"
                   },
                   "keyword2":{
                       "value":"请下午3点准时收看直播课程",
                       "color":"#173177"
                   },
                   "keyword4": {
                       "value":"20181002",
                       "color":"#173177"
                   }
           }
       }';
    $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$access_token}";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
    curl_setopt($ch, CURLOPT_POST,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$formwork);
    $data = curl_exec($ch);
    curl_close($ch);
    echo $data;
    $this->index2();
}

public function index2(){
echo '
<hr/>
<pre>
//获取微信access_token
public  function getaccess_token(){
   //appid与appsecret改成你自己的
   $appid = \'wx9aacf08f3fbd2e37\';
   $appsecret = \'821d788d0fe0d79b5ae414d1a0b93325\';
   $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL,$url);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
   $data = curl_exec($ch);
   curl_close($ch);
   $data = json_decode($data,true);
   return $data[\'access_token\'];
}

//设置与发送模板信息
public function set_msg(){
 //获取access_token
 $access_token = $this->getaccess_token();
 //这里是在模板里修改相应的变量
 $formwork = \'{
        "touser":"oUwuW1e-P9EwJ1KsAaie7Ux3MN7E",
        "template_id":"DmJfYJLSuSFGdpsSFkGL3mNmglqZLNCZ494r0Z5Gfqk",
        "url":"http://www.wangwenxiao.com",            
        "data":{
                "keyword1": {
                    "value":"财税筹划",
                    "color":"#173177"
                },
                "keyword2":{
                    "value":"请下午3点准时收看直播课程",
                    "color":"#173177"
                },
                "keyword4": {
                    "value":"20181002",
                    "color":"#173177"
                }
        }
    }\';
 $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$access_token}";
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL,$url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
 curl_setopt($ch, CURLOPT_POST,1);
 curl_setopt($ch, CURLOPT_POSTFIELDS,$formwork);
 $data = curl_exec($ch);
 curl_close($ch);
 echo $data;
 return $this->fetch();
}



</pre>

';


}




}

